package com.ccx.coachsite.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import com.ccx.coachsite.R;
import com.ccx.coachsite.api.CoachSiteApiAccessor;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.downloadmgr.DownloadTool;
import com.ccx.coachsite.model.SoftShareMessage;
import com.ccx.coachsite.ui.login.LoginHelper;
import com.ccx.coachsite.ui.share.OpenShareDetail;


public class OpenShareTool {
	
	/**
	 * 登录成功对外广播
	 */
	public final static String LOGIN_SUCCESS_RECEIVER="coachsite.login.success.receiver";

	
	public static String getShareDesc(final Context mContext){
		if (mContext==null)
			return "分享";
		boolean isExport = SharedPreferencesUtil.getBoolean(mContext, SharedKey.Shared_Is_Expert);
		return isExport?"推荐":"分享";
	}
	
	/**
	 * 分享功能
	 * @param mContext
	 * @param dialogTitle 应用标题
	 * @param downUrl     应用下载地址
	 * @param infoID	       应用ID
	 * @param shareType   应用类型  1.教程 2应用 3游戏 4专题
	 * @param category    教程分类(兼做搜索分享是的标志位,当传“search”表示是搜索分享)
	 */
	public static void showOperDialog(final Context mContext, final String dialogTitle,final String downUrl, final String infoID, final int shareType, final String category, final String softFlag)
	{
		if (!LoginHelper.requireLogin(mContext))
			return;
		
		String mUserId = null;
		mUserId = SharedPreferencesUtil.getValue(mContext, SharedKey.Shared_UserId_Key, mUserId);
		final String userID = mUserId;
		
		boolean isExport = SharedPreferencesUtil.getBoolean(mContext, SharedKey.Shared_Is_Expert);
		final int itemCount = isExport?2:1;
		
		String weiboShareText = "";
		
		//1.教程 2应用 3游戏 4专题
		switch (shareType) {
		case SoftShareMessage.TYPE_TUTORIAL_SHARE_TYPE:
			weiboShareText = mContext.getString(R.string.soft_mgr_share_weibo_tutorial);
			break;
		case SoftShareMessage.TYPE_APP_SHARE_TYPE:
			weiboShareText = mContext.getString(R.string.soft_mgr_share_weibo_soft);
			break;
		case SoftShareMessage.TYPE_GAME_SHARE_TYPE:
			weiboShareText = mContext.getString(R.string.soft_mgr_share_weibo_game);
			break;
		case SoftShareMessage.TYPE_TOPIC_SHARE_TYPE:
			weiboShareText = mContext.getString(R.string.soft_mgr_share_weibo_topic);
			break;
		default:
			break;
		}
		
		weiboShareText=weiboShareText.replace("{0}", CoachSiteApiAccessor.APP_DOWN_URL);
		weiboShareText=weiboShareText.replace("{1}", dialogTitle+downUrl);
		final String shareText = weiboShareText;	
		
		String[] strItems=new String[itemCount];
		if (itemCount==2) {
			strItems[0]="消息推送";
			strItems[1]="免费短信";
			//如果是搜索分享,直接跳转到免费短信分享
			if ( "search".equals(category) ) {
				Intent intent=new Intent(mContext,OpenShareDetail.class);
				intent.putExtra("infoID", infoID);
				intent.putExtra("shareType", shareType);
				intent.putExtra("infoName", dialogTitle);
				intent.putExtra("category", category);
				intent.putExtra("currentType", 2);
				intent.putExtra(DownloadTool.SOFT_FLAG, softFlag);
				mContext.startActivity(intent);
				
				return;
			}
		}else{
			strItems[0]="免费短信";
			
			Intent intent=new Intent(mContext,OpenShareDetail.class);
			intent.putExtra("infoID", infoID);
			intent.putExtra("shareType", shareType);
			intent.putExtra("infoName", dialogTitle);
			intent.putExtra("category", category);
			intent.putExtra("currentType", 2);
			intent.putExtra(DownloadTool.SOFT_FLAG, softFlag);
			mContext.startActivity(intent);
			
			return;
		}
		
		new AlertDialog.Builder(mContext).setTitle(getShareDesc(mContext)+"到")
						.setItems(strItems, new DialogInterface.OnClickListener() {
			
					@Override
					public void onClick(DialogInterface dialog, int which) {
						switch(which)
						{
						
						case -2:
							OpenShareSinaWeiBo sinaWeiBo = new OpenShareSinaWeiBo(mContext);
							sinaWeiBo.shareSinaWeiBo("com.sina.weibo",shareText, userID);
							break;
						case -1:
							//share(mContext,"com.tencent.WBlog",shareText);
							OpenShareQQWeiBo qqWeiBo = new OpenShareQQWeiBo(mContext);
							qqWeiBo.shareQQWeiBo("com.tencent.WBlog",shareText, userID);	
							break;
					
						case 0:
							if (itemCount==2) {
								Intent intent = new Intent();
								intent.setClass(mContext, OpenShareDetail.class);
								intent.putExtra("infoID", infoID);
								intent.putExtra("shareType", shareType);
								intent.putExtra("infoName", dialogTitle);
								intent.putExtra("category", category);
								intent.putExtra(DownloadTool.SOFT_FLAG, softFlag);
								mContext.startActivity(intent);
							}else{
								Intent intent=new Intent(mContext,OpenShareDetail.class);
								intent.putExtra("infoID", infoID);
								intent.putExtra("shareType", shareType);
								intent.putExtra("infoName", dialogTitle);
								intent.putExtra("category", category);
								intent.putExtra("currentType", 2);
								intent.putExtra(DownloadTool.SOFT_FLAG, softFlag);
								mContext.startActivity(intent);
							}
							break;
						case 1:
							Intent intent=new Intent(mContext,OpenShareDetail.class);
							intent.putExtra("infoID", infoID);
							intent.putExtra("shareType", shareType);
							intent.putExtra("infoName", dialogTitle);
							intent.putExtra("category", category);
							intent.putExtra("currentType", 2);
							intent.putExtra(DownloadTool.SOFT_FLAG, softFlag);
							mContext.startActivity(intent);
							
							break;	
						}
					}
				}).create().show();
				
	}
	
	/**
	 * 分享
	 */
	/*
	private static void share(Context mContext, String targetPackage, String appName) {
		
		if ( !isInstallAPK(mContext, targetPackage) ){
			downApp(mContext, appName);
			return ;
		}
		
		String content="嗨, 我找到了一个非常好看的软件。它的下载地址:{0}#";
		content=content.replace("{0}", "url地址");
		
		Intent it = new Intent(Intent.ACTION_SEND);
		String[] tos = { "" };
		it.setPackage(targetPackage);
		it.putExtra(Intent.EXTRA_EMAIL, tos);
		it.putExtra(Intent.EXTRA_TEXT, content);
		it.putExtra(Intent.EXTRA_SUBJECT, "应用助理分享");
		it.setType("text/plain");
		it.putExtra("sms_body", content);
		mContext.startActivity(Intent.createChooser(it, ""));
	}
	
	//android查询指定的程序是否安装上 查询安装包 
	public static boolean isInstallAPK(Context mContext, String packAgeName){
		
		boolean bReturn = true;
		
		PackageManager packageManager= mContext.getPackageManager();
		
		try {
			packageManager.getApplicationInfo(packAgeName, PackageManager.GET_META_DATA);
		} catch (NameNotFoundException e) {			
			e.printStackTrace();
			bReturn = false;
			return bReturn; 
		}
		
		return bReturn;
	}
	
	private static void downApp(final Context mContext, final String appName){
		
		AlertDialog.Builder renameDialog=new AlertDialog.Builder(mContext);
		renameDialog.setTitle("应用助理")
		.setMessage(" <"+appName+"> 您还没有安装,现在就去下载？")
		.setPositiveButton("去下载", new DialogInterface.OnClickListener() {	
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent( CoachSiteMainActivity.ACTION_SEARCH_NAME );
				intent.putExtra("keyword", appName);
				mContext.sendBroadcast( intent );
			}
		})
		.setNeutralButton("取消", null)
		.create().show();	
	}
	
	*/
}
